----------------------------------------------------------------------------------------------------------------------------------------------------
--[[
    玲珑饰物： 【 Exquisite Ornament 】
    胡桃专属头饰。耐久类型为矿工帽型，戴在头上才会损失耐久。满耐久情况下可以戴7天。
    胡桃佩戴时，增加147生命上限，42理智上限，0.42火伤系数，0.42攻击系数，84%防水，免疫雷劈    
    玲珑饰物可以右键种在地上变成梅花，与种植梅花的逻辑完全相同。
]]--
----------------------------------------------------------------------------------------------------------------------------------------------------
---
    local assets =
    {
        Asset("ANIM", "anim/hutao_equipment_exquisite_ornament.zip"),
    }
----------------------------------------------------------------------------------------------------------------------------------------------------
-- 参数
    local MAX_FINITE_USES = 7*480           --- 最大使用次数

----------------------------------------------------------------------------------------------------------------------------------------------------
---
    local function onequip(inst, owner)
        -----------------------------------------------------
        --- 检查tag
            if not owner:HasTag("hutao") then
                return
            end
        -----------------------------------------------------
        -- --- 灯光
        --     if inst.light_fx then
        --         inst.light_fx:Remove()
        --     end
        --     inst.light_fx = owner:SpawnChild("minerhatlight")
        -----------------------------------------------------
        --- 计时器
            if inst._finiteuses_task then
                inst._finiteuses_task:Cancel()
            end
            inst._finiteuses_task = inst:DoPeriodicTask(1,function()
                inst.components.finiteuses:Use()
            end)
        -----------------------------------------------------
        --- 属性配置
            inst:DoTaskInTime(0.1,function()
                if owner.components.hutao_com_max_value_controller then
                    owner.components.hutao_com_max_value_controller:AddTempExtraHealth(inst,147)
                    owner.components.hutao_com_max_value_controller:AddTempExtraSanity(inst,42)
                end
                if owner.components.hutao_com_fire_damage then
                    owner.components.hutao_com_fire_damage:AddAdditiveMultiplier(inst,0.42)
                end
                if owner.components.combat then
                    owner.components.combat.externaldamagemultipliers:SetModifier(inst,1+0.42)
                end
                if owner.components.moisture then
                    owner.components.moisture.waterproofnessmodifiers:SetModifier(inst,0.84)
                end
            end)
        -----------------------------------------------------
        --- 
            owner.AnimState:OverrideSymbol("hair_hat", "hutao_equipment_exquisite_ornament", "hair_hat")
            owner.AnimState:OverrideSymbol("headbase_hat", "hutao_equipment_exquisite_ornament", "headbase_hat")
            owner.AnimState:Show("HAT")
            owner.AnimState:Show("HAIR_HAT")
            owner.AnimState:Hide("HEAD")
            owner.AnimState:Show("HEAD_HAT")
        -----------------------------------------------------
    end

    local function onunequip(inst, owner)
        -----------------------------------------------------
        --- 灯光
            if inst.light_fx then
                inst.light_fx:Remove()
                inst.light_fx = nil
            end
        -----------------------------------------------------
        --- 计时器
            if inst._finiteuses_task then
                inst._finiteuses_task:Cancel()
                inst._finiteuses_task = nil
            end
        -----------------------------------------------------
        --- 属性配置
            if owner.components.hutao_com_max_value_controller then
                owner.components.hutao_com_max_value_controller:RemoveTempExtraHealth(inst)
                owner.components.hutao_com_max_value_controller:RemoveTempExtraSanity(inst)
            end
            if owner.components.hutao_com_fire_damage then
                owner.components.hutao_com_fire_damage:RemoveAdditiveMultiplier(inst)
            end
            if owner.components.combat then
                owner.components.combat.externaldamagemultipliers:RemoveModifier(inst)
            end
            if owner.components.moisture then
                owner.components.moisture.waterproofnessmodifiers:RemoveModifier(inst)
            end            
        -----------------------------------------------------
        ---
            owner.AnimState:ClearOverrideSymbol("hair_hat")
            owner.AnimState:ClearOverrideSymbol("headbase_hat")
            owner.AnimState:Hide("HAT")
            owner.AnimState:Hide("HAIR_HAT")
            owner.AnimState:Show("HEAD")
            owner.AnimState:Hide("HEAD_HAT")
        -----------------------------------------------------
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 种植
    local function OnDeploy(inst, pt, deployer)
        local flower = SpawnPrefab("planted_flower")
        if flower then
            if flower.components.hutao_com_flower_switcher then
                flower.components.hutao_com_flower_switcher:Switch("plum_blossom")
            end
            flower.Transform:SetPosition(pt:Get())
            if deployer and deployer.SoundEmitter then
                deployer.SoundEmitter:PlaySound("dontstarve/common/plant")
            end
            inst:Remove()
        end
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 本体
    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddNetwork()

        MakeInventoryPhysics(inst)

        inst.AnimState:SetBank("hutao_equipment_exquisite_ornament")
        inst.AnimState:SetBuild("hutao_equipment_exquisite_ornament")
        inst.AnimState:PlayAnimation("idle")


        MakeInventoryFloatable(inst)

        inst.entity:SetPristine()

        --------------------------------------------------------------------------
        if not TheWorld.ismastersim then
            return inst
        end
        -----------------------------------------------------

        -----------------------------------------------------
        --- 物品
            inst:AddComponent("inspectable")
            inst:AddComponent("inventoryitem")
            -- inst.components.inventoryitem:ChangeImageName("cane")
            inst.components.inventoryitem.imagename = "hutao_equipment_exquisite_ornament"
            inst.components.inventoryitem.atlasname = "images/inventoryimages/hutao_equipment_exquisite_ornament.xml"
        -----------------------------------------------------
        --- 装备
            inst:AddComponent("equippable")
            inst.components.equippable:SetOnEquip(onequip)
            inst.components.equippable:SetOnUnequip(onunequip)
            inst.components.equippable.equipslot = EQUIPSLOTS.HEAD
            inst.components.equippable.restrictedtag = "hutao"
            inst.components.equippable.insulated = true --- 免疫雷劈
        -----------------------------------------------------
        --- 耐久度
            inst:AddComponent("finiteuses")
            inst.components.finiteuses:SetMaxUses(MAX_FINITE_USES)
            inst.components.finiteuses:SetUses(MAX_FINITE_USES)
            inst.components.finiteuses:SetOnFinished(inst.Remove)
        -----------------------------------------------------
        --- 作祟
            MakeHauntableLaunch(inst)
        ---------------------------------------------------------
        --- 种植
            inst:AddComponent("deployable")
            inst.components.deployable.ondeploy = OnDeploy
            inst.components.deployable:SetDeployMode(DEPLOYMODE.PLANT)
            inst.components.deployable:SetDeploySpacing(DEPLOYSPACING.LESS)
        ---------------------------------------------------------
        return inst
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- placer
    local function placer_postinit_fn(inst)
        
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
return Prefab("hutao_equipment_exquisite_ornament", fn, assets),
    MakePlacer("hutao_equipment_exquisite_ornament_placer", "hutao_plants_plum_blossom_flower", "hutao_plants_plum_blossom_flower", "plum_blossom", nil, nil, nil, nil, nil, nil, placer_postinit_fn, nil, nil)
